/**
 * Created by crossApp on 16/8/8.
 */
var AutoCollectionViewVerticalTest = ca.CAViewController.extend({

        ctor: function () {
            this._super();

            this.HorizontalcolorArr = new Array();
            this._randomColor() ;

        },

        viewDidLoad: function() {
            var headerRefreshView = ca.CAPullToRefreshView.create(ca.CAPullToRefreshView.Type.Header);
            var footerRefreshView = ca.CAPullToRefreshView.create(ca.CAPullToRefreshView.Type.Footer);

            this.autoCollection = ca.CAAutoCollectionView.createWithLayout(ca.DLayout.fill());
            this.autoCollection.setAllowsSelection(true);
            this.autoCollection.setAllowsMultipleSelection(true);
            this.autoCollection.setHeaderRefreshView(headerRefreshView) ;
            this.autoCollection.setFooterRefreshView(footerRefreshView) ;

            this.autoCollection.onDidSelectCellAtIndexPath(this.collectionViewDidSelectCellAtIndexPath.bind(this));
            this.autoCollection.onCellAtIndexPath(this.collectionCellAtIndex.bind(this));
            this.autoCollection.onCellSizeAtIndexPath(this.collectionViewSizeForItemAtIndexPath.bind(this));
            this.autoCollection.onNumberOfItemsAtIndexPath(this.numberOfItemsInSection.bind(this));
            this.autoCollection.onNumberOfSections(this.numberOfSections.bind(this));
            this.autoCollection.onHeaderBeginRefreshing(this.scrollViewHeaderBeginRefreshing.bind(this)) ;
            this.autoCollection.onFooterBeginRefreshing(this.scrollViewFooterBeginRefreshing.bind(this)) ;

            this.autoCollection.setHoriCellInterval(20);
            this.autoCollection.setVertCellInterval(20);
            this.getView().addSubview(this.autoCollection);

        },


        _randomColor:function(){
            for (var i = 0; i < 60; i++)
            {
                var r = Math.floor(Math.random()*255);
                var g = Math.floor(Math.random()*255);
                var b = Math.floor(Math.random()*255);

                this.HorizontalcolorArr.push(ca.CAColor4B.set(r, g, b, 255));
            }
        },

        refreshDataHeader: function( interval){
            this.HorizontalcolorArr = [];
            this._randomColor() ;
            this.autoCollection.reloadData();
        },
        refreshDataFooter: function(interval){
            this._randomColor() ;
            this.autoCollection.reloadData();
        },

        scrollViewHeaderBeginRefreshing: function()
        {
            ca.CAScheduler.getScheduler().scheduleOnce(this.refreshDataHeader.bind(this), "A",this, 0.5);
        },
        scrollViewFooterBeginRefreshing: function()
        {
            ca.CAScheduler.getScheduler().scheduleOnce(this.refreshDataFooter.bind(this), "A",this, 0.5);
        },
        collectionViewDidSelectCellAtIndexPath: function( section, item)
        {
            var cell = this.autoCollection.cellForRowAtIndexPath(section, item);
            cell.getContentView().setRotation(-360);
            cell.getContentView().setScale(0.5);
            ca.CAViewAnimation.beginAnimations("");
            cell.getContentView().setRotation(0);
            cell.getContentView().setScale(1.0);
            ca.CAViewAnimation.commitAnimations();
        },

        collectionViewDidDeselectCellAtIndexPath: function ( section, item)
        {
        },

        collectionCellAtIndex: function(cellSize, section, item)
        {
            var cell = this.autoCollection.dequeueReusableCellWithIdentifier("CrossApp");

            if (cell == null)
            {
                cell = ca.CACollectionViewCell.create("CrossApp");

                var itemImage = ca.CAView.createWithLayout(ca.DLayout.fill());
                itemImage.setTag(99);
                cell.getContentView().addSubview(itemImage);

                var itemText = ca.CALabel.createWithLayout(ca.DLayout.fill());
                itemText.setTag(100);
                itemText.setFontSize(29);
                itemText.setTextAlignment(ca.CATextAlignment.Center);
                itemText.setVerticalTextAlignmet(ca.CAVerticalTextAlignment.Center);
                cell.getContentView().addSubview(itemText);
            }

            var itemImageView = cell.getContentView().getSubviewByTag(99);
            itemImageView.setColor(this.HorizontalcolorArr[item]);
            log("row = "+ item);


            var itemText = cell.getContentView().getSubviewByTag(100);
            itemText.setText("("+section+","+item+")");
            return  cell;

        },

        collectionViewSizeForItemAtIndexPath: function(  section, item)
        {
            return ca.DSize.set(230, 230);
        },

        numberOfItemsInSection: function( section)
        {
            return this.HorizontalcolorArr.length;
        },
        numberOfSections: function()
        {
            return 1;
        }

    });
